package com.hyb.algorithm.data.struct.dp;

/**
 * @author: ybhu
 * @create: 2021/09/21 14:58
 * <p>
 * 673. 最长递增子序列的个数
 * https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/
 */
public class FindNumberOfLIS {

    public static void main(String[] args) {
        FindNumberOfLIS exe = new FindNumberOfLIS();
        int[] nums = new int[]{2, 2, 2, 2, 2};
        int ret= exe.findNumberOfLIS(nums);

        System.out.println(ret);
    }

    public int findNumberOfLIS(int[] nums) {

        int n = nums.length;
        int[] dp = new int[n];
        int[] cnt = new int[n];
        int res = 0;

        int maxLen = 0;

        for (int i = 0; i < n; i++) {
            dp[i] = 1;
            cnt[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {

                    if (dp[j] + 1 > dp[i]) {
                        // 重置计数
                        cnt[i] = cnt[j];
                    } else if (dp[j] + 1 == dp[i]) {
                        cnt[i] += cnt[j];
                    }


                    dp[i] = Math.max(dp[j] + 1, dp[i]);

                }
            }

            if (dp[i] > maxLen) {
                maxLen = dp[i];
                res = cnt[i]; // 重置计数
            } else if (dp[i] == maxLen) {
                res += cnt[i];
            }

        }
        return res;
    }
}
